define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

            $(document).on('click', '.btn-import', function () {
                Backend.api.open('hongruan/codes/import', '导入激活码');
            });
            $(document).on('click', '.btn-batch-distribute', function () {
                Backend.api.open('hongruan/codes/distribute', '批量分配');
            });
        },
        table: {
            first: function () {
                // 未分配
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'hongruan/codes/unallocated',
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'state', checkbox: true,},
                            {field: 'id', title: 'ID'},
                            {field: 'code', title: __('激活码'),},
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: table1,
                                events: Table.api.events.operate,
                                buttons: [{
                                    name: 'distribute',
                                    text: __('分配'),
                                    icon: 'fa fa-check-circle-o',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'hongruan/codes/distribute',
                                }],
                                formatter: Table.api.formatter.operate
                            },
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 已分配
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'hongruan/codes/allocated',
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'id', title: __('ID')},
                            {field: 'admin.username', title: __('分配帐号')},
                            {field: 'admin.nickname', title: __('联系人')},
                            {field: 'admin.mobile', title: __('手机号')},
                            {field: 'count', title: __('分配数量'),},
                            {field: 'used_count', title: __('已激活数量'),},
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: table2,
                                events: Table.api.events.operate,
                                buttons: [{
                                    name: '详情',
                                    text: __('详情'),
                                    icon: 'fa fa-check-circle-o',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'hongruan/codes/detail',
                                }],
                                formatter: Table.api.formatter.operate
                            },

                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            }
        },
        detail: function () {
            // 初始化表格参数配置
            Table.api.init();

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table_1[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table_1: {
            third: function () {
                // 未分配
                var table3 = $("#table3");
                table3.bootstrapTable({
                    url: 'hongruan/codes/notActivate/ids/' + Config.admin.ids,
                    toolbar: '#toolbar3',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'state', checkbox: true,},
                            {field: 'id', title: 'ID'},
                            {field: 'code', title: __('激活码'),},
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table3);
            },
            fourth: function () {
                // 已分配
                var table4 = $("#table4");
                table4.bootstrapTable({
                    url: 'hongruan/codes/activate/ids/' + Config.admin.ids,
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar4',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'id', title: __('ID')},
                            {field: 'code', title: __('激活码')},
                            {field: 'device', title: __('激活设备')},
                            {field: 'brand', title: __('激活品牌')},
                            {
                                field: 'activated_at',
                                title: __('激活时间'),
                                sortable: true,
                                formatter: Table.api.formatter.datetime,
                                operate: 'RANGE',
                                addclass: 'datetimerange'
                            },
                            {field: 'school.name', title: __('绑定学校'),},
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table4);
            }
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        import: function () {
            Controller.api.bindevent();
        },
        distribute: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});